/*
by txdnet.cn tonydon
 */

package txdnet.util.uploader.apache.fileupload;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import txdnet.util.FileUtil;
import txdnet.util.Logger;
/**
 *
 * @author txdnet
 */
public class SingleFileUpload extends FileUploadBase{

    private FileItem fileItem;

    /**
     *
     * @param request
     * @throws UnsupportedEncodingException
     */
    public void parseRequest(HttpServletRequest request){

        DiskFileItemFactory factory = new DiskFileItemFactory();

        factory.setSizeThreshold(sizeThreshold);

        if (repository != null)
            factory.setRepository(repository);

        ServletFileUpload upload = new ServletFileUpload(factory);

        upload.setHeaderEncoding(encoding);

        try{
            List<FileItem> items = upload.parseRequest(request);

            for (FileItem item : items){
                if (item.isFormField()){
                    parameters.put(item.getFieldName(), item.getString(encoding));
                } else{
                    if (super.isValidFile(item)){
                        if (fileItem == null)fileItem = item;
                    }
                }
            }

        } catch (Exception ex){
            Logger.error(SingleFileUpload.class, ex.toString());
        }
    }

    /**
     * 上传文件, 调用该方法之前必须先调用 parseRequest(HttpServletRequest request)
     * @param fileName 完整文件路径
     * @throws Exception
     */
    public void upload(String fileName) throws Exception{
        if (fileItem == null)return;
        File file = new File(fileName);
        uploadFile(file);
    }

    /** *//** *//** *//**
     * 上传文件, 调用该方法之前必须先调用 parseRequest(HttpServletRequest request)
     * @param parent 存储的目录
     * @throws Exception
     */
    public void upload(File parent) throws Exception{
        if (fileItem == null)return;
        File file = new File(parent, FileUtil.getFileName(fileItem.getName()));
        uploadFile(file);
    }

    private void uploadFile(File file) throws Exception{
        long fileSize = fileItem.getSize();
        if (super.sizeMax > -1 && fileSize > super.sizeMax){
            String message = String.format("the request was rejected because its size (%1$s) exceeds the configured maximum (%2$s)", fileSize, super.sizeMax);
          throw new org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException(message, fileSize, super.sizeMax);
        }

   //  fileItem.getName();
        fileItem.write(file);
        if(!fileItem.isInMemory()){
            fileItem.delete();
        }
    }

    /**
     * 获取文件信息
     * 必须先调用 parseRequest(HttpServletRequest request)
     * @return
     */
    public FileItem getFileItem(){
        return fileItem;
    }
}